/**
 * 个人信息管理
 */
$package("personInfo");
personInfo = {
    /**
     * 初始化方法
     */
    initPersonInfoPage: function () {
        this.obj = {
            peInfoFormObj: $('#personInfoForm'), //用户form表单
            editPersonBtn: $('#editPerson'),  //编辑用户按钮
            updatePsdBtn: $('#updatePsd'),  //修改密码按钮
            submitDataBtn: $('#submitPerson')  //提交按钮
        };
        this.domVar = {
            sexDic: [],
            sexArray: []

        };
        this.initDict();//加载数据字典
        this.btnPersonEvent();//按钮事件
        this.initPersonDom();

    },

    initDict : function () {
        /**************加载数据字典**************/
        //*********获取用户删除状态字典**********
        $.ajax({
            url: wwwroot + 'base/dictionary/getByCode/unitFlag',
            type: 'GET',
            cache: false,
            dataType: 'json',
            success: function (result) {
                if (result.respCode == "200") {
                    $.each(result.dataObj, function (i, item) {
                        $('#personInfoForm select[name="userFlag"]').append('' +
                            '<option value="' + item.dicCode + '">' + item.dicName + '</option>');
                    });
                }
            }
        });
        //*********获取用户性别字典**********
        $.ajax({
            url: wwwroot + 'base/dictionary/getByCode/sex',
            type: 'GET',
            cache: false,
            dataType: 'json',
            success: function (result) {
                if (result.respCode == "200") {
                    $.each(result.dataObj, function (i, item) {
                        $('#personInfoForm select[name="sex"]').append('' +
                            '<option value="' + item.dicCode + '">' + item.dicName + '</option>');
                    });
                }
            }
        });
        //*********获取用户审核状态字典**********
        $.ajax({
            url: wwwroot + 'base/dictionary/getByCode/userAuditFlag',
            type: 'GET',
            cache: false,
            dataType: 'json',
            success: function (result) {
                if (result.respCode == "200") {
                    $.each(result.dataObj, function (i, item) {
                        $('#personInfoForm select[name="auditFlag"]').append('' +
                            '<option value="' + item.dicCode + '">' + item.dicName + '</option>');
                    });
                }
            }
        });
    },

    initPersonDom: function ()   {
        var self = this;
        self.obj.submitDataBtn.hide();
        $.ajax({
            url: wwwroot + 'base/user/self',
            success: function (result) {
                if (result.respCode == "200") {
                    var data = result.dataObj;
                    console.log(data);
                    data.applyTime = new Date().format();
                    data.auditTime = new Date().format();
                    data.createTime = new Date().format();
                    data.modifyTime = new Date().format();
                    self.obj.peInfoFormObj.setForm(data);
                    //当前用户角色
                    var userRoleList = data.userRoleList;
                    //所有用户角色
                    var allRoleList = data.allRoleList;
                    //日志信息集合
                    var userLogList = data.userLogList;
                    var userRole = [];
                    if (userRoleList && userRoleList.length > 0) {
                        $.each(userRoleList, function (i, item) {
                            userRole[item] = true;
                        })
                    }
                    $('#userRoleInfo').html('');
                    if (allRoleList && allRoleList.length > 0) {
                        $.each(allRoleList, function (i, item) {
                            if (userRole[item.id]) {
                                $('#userRoleInfo').append('<li class="col-sm-5 col-sm-offset-1"><label>' +
                                    '<input  type="checkbox" disabled="disabled" checked="checked" value="' + item.id + '" name="userRoleEdit"/>' +
                                    item.roleName + '(' + item.roleCode + ')' + '</label></li>');
                            } else {
                                $('#userRoleInfo').append('<li class="col-sm-5 col-sm-offset-1"><label>' +
                                    '<input type="checkbox" disabled="disabled" value="' + item.id + '" name="userRoleEdit"/>' +
                                    item.roleName + '(' + item.roleCode + ')' + '</label></li>');
                            }
                        });
                    }
                    $('#auditRecodes').html('');
                    if (userLogList && userLogList.length > 0) {
                        $.each(userLogList, function (i, item) {
                            $('#auditRecodes').append('操作人：' + item.operatUserAccount + '时间：' + item.createTime + '结果：' + item.operatResult + '描述信息：' + item.operatDesc + "\n");
                        });
                    }
                }
            }
        });
    },
    btnPersonEvent: function () {
        var self = this;
        //编辑
        self.obj.editPersonBtn.unbind().bind('click', function () {
            self.obj.submitDataBtn.show();
            self.obj.editPersonBtn.hide();
            $('#personInfoForm input[name="realName"]').attr('readonly', false);
            $('#personInfoForm input[name="idCard"]').attr('readonly', false);
            $('#personInfoForm input[name="emailAddress"]').attr('readonly', false);
            $('#personInfoForm input[name="phoneNum"]').attr('readonly', false);
            $('#personInfoForm input[name="nickname"]').attr('readonly', false);
            $('#personInfoForm input[name="realName"]').focus();
        });

        //修改密码
        this.obj.updatePsdBtn.unbind().bind('click', function () {
            sckWindow({
                size: 'md',
                url: 'tpl/base/userCenter/updatePsd.html',
                sureBtn: true,
                onSureClick: function () {
                    if ($('#updatePsdForm').formVerify()) {
                        var userPwd = $('#updatePsdForm').getForm();
                        if (userPwd["newPwd"] != userPwd["rePwd"]) {
                            skyAlert("两次输入的新密码不正确！");
                        } else {
                            $.ajax({
                                url: wwwroot + 'base/user/passWord',
                                method: 'PUT',
                                data: JSON.stringify(userPwd),
                                headers: {'Content-type': 'application/json;character=UTF-8'},
                                success: function (result) {
                                    var strMsg = "密码修改失败，原密码不正确！";
                                    if (result.respCode == '201') {
                                        strMsg = '密码修改成功！';
                                        closeSkyWindow();
                                    }
                                    skyAlert(strMsg);
                                }
                            });
                        }
                    }
                },
                afterWinOpen: function () {
                }
            });
        });
        //提交
        self.obj.submitDataBtn.unbind().bind('click', function () {
            if (self.obj.peInfoFormObj.formVerify()) {
                var personObj = self.obj.peInfoFormObj.getForm();
                $.ajax({
                    method: 'PUT',
                    dataType: 'json',
                    url: wwwroot + 'base/user/info',
                    data: JSON.stringify(personObj),
                    headers: {'Content-type': 'application/json;character=UTF-8'},
                    success: function (result) {
                        var strMsg = "修改用户信息成功！";
                        if ('201' != result.respCode) {
                            strMsg = "修改用户信息失败！";
                        }
                        skyAlert(strMsg);
                        $('#personInfoForm input[name="realName"]').attr('readonly', true);
                        $('#personInfoForm input[name="idCard"]').attr('readonly', true);
                        $('#personInfoForm input[name="emailAddress"]').attr('readonly', true);
                        $('#personInfoForm input[name="phoneNum"]').attr('readonly', true);
                        $('#personInfoForm input[name="nickname"]').attr('readonly', true);
                        self.obj.submitDataBtn.hide();
                        self.obj.editPersonBtn.show();
                    }
                });

            }
        });
    }
};
$(function () {
    personInfo.initPersonInfoPage();
});